import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {BaseService} from '../../../shared/service/base.service';
import {ApiUrl} from '../../../config/api-url';
import {PageHelper} from '../../../utils/page-helper-model';
import { differenceInYears, differenceInMonths, differenceInDays, differenceInHours, differenceInMinutes, differenceInSeconds } from 'date-fns';

@Component({
  selector: 'app-device-online-offline',
  templateUrl: './device-online-offline.component.html',
  styleUrls: ['./device-online-offline.component.less']
})
export class DeviceOnlineOfflineComponent implements OnInit {
  @Input()
  deviceOnlineOfflineVisible = false;
  @Input()
  device: any;
  @Output()
  emitVisible: EventEmitter<any> = new EventEmitter();
  // 分页
  pageHelper: PageHelper = new PageHelper;
  listOfData: any;
  title:any;

  constructor(private baseService: BaseService) { }

  ngOnInit(): void {

  }
  ngOnChanges(): void{
    // console.log('device',this.device);
    if(this.device != null || this.device != undefined){
      this.title = this.device?.deviceNo + '上线、离线时间点';
      this.deviceOnlineOfflineByDeviceNoList();
    }
  }
  handleCancel(){
    this.deviceOnlineOfflineVisible = false;
    this.pageHelper.currentPage = 1;
    this.emitVisible.emit(this.deviceOnlineOfflineVisible);
  }
  nzPageIndexChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.deviceOnlineOfflineByDeviceNoList();
  }
  calculateTimeDifference(startTime, endTime, type, index) {
    // console.log("startTime",startTime);
    // console.log("endTime",endTime);
    // console.log("this.pageHelper.currentPage",this.pageHelper.currentPage);
    if(index > 0 && endTime == null){
      return '';
    }
    if(this.pageHelper.currentPage > 1 && endTime == null){
      return '';
    }
    var startDate = new Date(startTime);
    var endDate = null;
    if(endTime == null || endTime == ''){
       endDate = new Date();
    }else{
       endDate = new Date(endTime);
    }
    var timeDiff = Math.abs(endDate.getTime() - startDate.getTime());

    var years = Math.floor(timeDiff / (1000 * 60 * 60 * 24 * 365));
    var months = Math.floor((timeDiff % (1000 * 60 * 60 * 24 * 365)) / (1000 * 60 * 60 * 24 * 30));
    var days = Math.floor((timeDiff % (1000 * 60 * 60 * 24 * 30)) / (1000 * 60 * 60 * 24));
    var hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((timeDiff % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((timeDiff % (1000 * 60)) / 1000);

    let name = '';
    if(type == 1){
      name += '在线时长';
    }else{
      name += '离线时长';
    }
    if(years > 0){
      name += years + '年';
    }
    if(months > 0){
      name+= months + '个月';
    }
    if(days > 0){
      name += days + '天';
    }
    if(hours > 0){
      name += hours + '小时';
    }
    if(minutes > 0){
      name += minutes + '分';
    }
    if(seconds > 0){
      name += seconds + '秒';
    }
    return name;
  }

  deviceOnlineOfflineByDeviceNoList() {
    if(this.device.deviceNo == null || this.device.deviceNo == ''){
      this.baseService.layer.messageError('设备号不能为空');
      return;
    }
    let params = {
      deviceNo : this.device.deviceNo,
    };
    this.baseService.findListByMuchCondition(ApiUrl.DeviceOnlineOfflineRecordUrl + '/deviceOnlineOfflineList', params, this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.pageHelper.totalItems = data.data.total;
          this.listOfData = data.data.list;
          for (let i = 0; i < this.listOfData.length; i++) {
            let timeDifference = this.calculateTimeDifference(this.listOfData[i].startTime, this.listOfData[i].endTime, this.listOfData[i].type, i);
            this.listOfData[i].timeDifference = timeDifference;
          }
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

}
